package com.gaofans.nrpc.common.processor;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author GaoFans
 */
public class CommonEventProcessor<T,E extends Serializable> implements EventProcessor<T,E> {

    private final ConcurrentHashMap<E,EventListener<T>> map;

    public CommonEventProcessor() {
        this.map = new ConcurrentHashMap<>();
    }

    public CommonEventProcessor(int size) {
        this.map = new ConcurrentHashMap<>(size);
    }

    @Override
    public void register(E id, EventListener<T> listener) {
        map.put(id,listener);
    }

    @Override
    public EventListener<T> unRegister(E id) {
        return map.remove(id);
    }

    @Override
    public EventListener<T> get(E id) {
        return map.get(id);
    }

    @Override
    public Map<E, EventListener<T>> all() {
        return Collections.unmodifiableMap(map);
    }
}
